package interceptor;

import cn.china.xfood.service.WebinfoService;
import entity.Webinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;


public class AdminInterceptor implements HandlerInterceptor {


    @Autowired
    WebinfoService webinfoService;

//    public AdminInterceptor(WebinfoService webinfoService){
//        this.webinfoService = webinfoService;
//    }
//    private WebinfoService webinfoService;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行了TestInterceptor的preHandle方法");
        return true;//如果设置为false时，被请求时，拦截器执行到此处将不会继续操作
        //如果设置为true时，请求将会继续执行后面的操作
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {




        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String currentTime = sdf.format(new Date());

        Webinfo webinfo = webinfoService.selectByvisitTime(currentTime);


        Webinfo webinfo1 = new Webinfo();webinfo1.setVisittime(currentTime);webinfo1.setVisittimes(1);


        if (webinfo!=null){
            webinfo.setVisittimes(webinfo.getVisittimes()+1);
            webinfoService.updateByPrimaryKeySelective(webinfo);
        }else{
            webinfoService.insertSelective(webinfo1);
        }

        System.out.println("这是postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("这是afetrCompletion");
    }
}
